#!/usr/bin/perl

#import modules

#map generator
use Games::RolePlay::MapGen;

#xml module
use XML::Simple;

use Data::Dumper;






#main

# 1. load config

my $config = XMLin("config.xml");

# 1.5 prepare the export folder
mkdir($config->{'lpmud'}->{'domainfilepath'} . $config->{'lpmud'}->{'mapname'}) ;

# 2. create new map
my $rpmapobj = makenewmap($config);

# 3. import xml
my $xmlmap = XMLin($config->{'mapcreator'}->{'xmlfilepath'} . $config->{'lpmud'}->{'mapname'} . ".xml" , ForceArray => [ "closure" ] );

# 4. export zone files
exportzone($config,$xmlmap);

# 5. clean up

#save a copy of our config
my $xmltemp = XMLout( $config );
my $fh;
open($fh,">", $config->{'mapcreator'}->{'xmlfilepath'} . $config->{'lpmud'}->{'mapname'} . "-config.xml" );
print $fh $xmltemp;
close($fh);

print "Complete\n";








#create a new map and return it's object
sub makenewmap() {
	
	my ( $config ) = @_;
	
	#to start off, pick our sizes
	my $temp = $config->{'mapcreator'}->{'map'};
	$temp->{'x'}->{'actual'} = (int( rand( ( $temp->{'x'}->{'max'} - $temp->{'x'}->{'min'} ) ) ) + $temp->{'x'}->{'min'} );
	$temp->{'y'}->{'actual'} = (int( rand( ( $temp->{'y'}->{'max'} - $temp->{'y'}->{'min'} ) ) ) + $temp->{'y'}->{'min'} );
	print "Creating new map with width " . $temp->{'x'}->{'actual'} . " and height " . $temp->{'y'}->{'actual'} . ".\n";
	
	#create the map object
	$temp = $config->{'mapcreator'};
	my $map = Games::RolePlay::MapGen->new({
		tile_size    => $temp->{'map'}->{'tilesize'},
		bounding_box => $temp->{'map'}->{'x'}->{'actual'} . "x" . $temp->{'map'}->{'x'}->{'actual'},
		num_rooms    => $temp->{'rooms'}->{'number'}, 
		max_room_size => $temp->{'rooms'}->{'x'}->{'max'} . "x" . $temp->{'rooms'}->{'y'}->{'max'},
		min_room_size => $temp->{'rooms'}->{'x'}->{'min'} . "x" . $temp->{'rooms'}->{'y'}->{'min'}
	});
	
	#add the generator
	$map->set_generator("Basic");
	
	#and generate the maze
	$map->generate("map.txt");
	
	#export the image if required
	if ( $config->{'mapexport'}->{'png'} )
	{
		$map->set_exporter( "PNG" );
		$map->export($config->{'mapexport'}->{'pngfilepath'} . $config->{'lpmud'}->{'mapname'} . ".png");
		print "Map image saved: " . $config->{'mapexport'}->{'pngfilepath'} . $config->{'lpmud'}->{'mapname'} . ".png\n";
	}
	
	#save out the xml
	$map->set_exporter( "XML" );
	$map->export($config->{'mapcreator'}->{'xmlfilepath'} . $config->{'lpmud'}->{'mapname'} . ".xml");
	print "XML map saved: " . $config->{'mapcreator'}->{'xmlfilepath'} . $config->{'lpmud'}->{'mapname'} . ".xml\n";
	
	#now return the object
	return($map);
	
}



# convert the xml map and export the cell files
sub exportzone()
{	
	
	my ( $config , $xmlmap ) = @_;
	
	# the bulk of the work, iterate over each cell in the map
	# x,y 0,0 is top left.
	
	# for each row
	foreach my $row ( @{$xmlmap->{'map'}->{'row'}} )
	{
		
		# for each cell along the row
		foreach my $cell ( @{ $row->{'tile'} } )
		{
			
			#skip blanks
			if ( $cell->{'type'} eq "wall" )
			{
				next;
			}
			
			
			#create the basic room
			my $room = "";
			$room .= "\n";
			$room .= "#include <lib.h>\n";
			$room .= "#include ROOMS_H\n\n";
			$room .= "inherit LIB_ROOM;\n\n";
			$room .= "static void create() {\n";
			$room .= "    room::create();\n";
			$room .= "    SetAmbientLight(30);\n";
			
			#label rooms and corridor cells properly
			if ( $cell->{'type'} eq "corridor" )
			{
				$room .= "	SetShort(\"a corridor\");\n";
				$room .= "	SetLong(\"A corridor\");\n";
			}
			elsif ( $cell->{'type'} eq "room" )
			{
				$room .= "	SetShort(\"a room\");\n";
				$room .= "	SetLong(\"A room\");\n";
			}
			
			
			#now calculate the exits
			my %exitstemp;
			$exitstemp{'north'} = $cell->{'xpos'} . "x" . ( $row->{'ypos'} - 1 );
			$exitstemp{'east'} = ( $cell->{'xpos'} + 1 ) . "x" . $row->{'ypos'};
			$exitstemp{'south'} = $cell->{'xpos'} . "x" . ( $row->{'ypos'} + 1 );
			$exitstemp{'west'} = ( $cell->{'xpos'} - 1 ) . "x" . $row->{'ypos'};
			
			#and remove those that are blocked by walls
			foreach my $wall ( @{ $cell->{'closure'} } )
			{
				delete($exitstemp{$wall->{'dir'}});
			}
			
			#now add the exits to the file
			$room .= "	SetExits( ([ \n";
			my $worldpath = $config->{'lpmud'}->{'domainworldpath'} . $config->{'lpmud'}->{'mapname'} . "/room_";
			foreach my $newexit ( keys(%exitstemp) )
			{
				$room .= "		\"" . $newexit . "\" : \"" . $worldpath . $exitstemp{$newexit} . "\",\n";
			}
			$room =~ s/,\n$/\n/g;
			$room .= "	]) );\n";
			
			#and close off the room
			$room .= "}\n\n";
			$room .= "void init(){\n";
			$room .= "	::init();\n";
			$room .= "}\n";
			
			
			#now, write this room to disk.
			my $fh;
			open($fh,">", $config->{'lpmud'}->{'domainfilepath'} . $config->{'lpmud'}->{'mapname'} . "/room_" . $cell->{'xpos'} . "x" . $row->{'ypos'} . ".c");
			print $fh $room;
			close($fh);
			
		#end of cell (x) loop
		}
		
	# end of row (y) loop	
	}
	
}






















